import BaseChannel from "../../base/BaseChannel";
import BaseView from "../../../cfw/mvc/BaseView";
import BigExportPage1Item from "./BigExportPage1Item";
import GlobalEvent from "../../../cfw/event/GlobalEvent";
import { EventName } from "../../../public/Config";
import SDKManager from "../../SDKManager";
import { BattleID } from "../../SDKConfig";
import BigExportC, { ExportType } from "../BigExportC";


const { ccclass, property } = cc._decorator;

@ccclass
export default class BigExportPage1 extends BaseView {

	@property({ type: cc.Sprite, displayName: "scrollview$VSprite" })
	scrollview$VSprite: cc.Sprite = null;

	@property({ type: cc.ScrollView, displayName: "scrollview$VScrollView" })
	scrollview$VScrollView: cc.ScrollView = null;

	@property({ type: cc.Sprite, displayName: "trainHead$VSprite" })
	trainHead$VSprite: cc.Sprite = null;


	@property(cc.Prefab)
	prefab: cc.Prefab = null;

	@property(cc.Node)
	content: cc.Node = null;

	@property(cc.Node)
	back: cc.Node = null;

	@property(cc.Node)
	zhalan: cc.Node = null;

	@property(cc.Node)
	yanchong: cc.Node = null;

	@property([cc.Node])
	yan: cc.Node[] = [];

	@property(cc.Node)
	plane: cc.Node = null;

	protected model: BaseChannel;
	protected controller: BigExportC;
	protected itemList: BigExportPage1Item[] = []
	protected buffer: BigExportPage1Item[] = []
	onLoad() {

		// this.setTimeout(() => {
		// 	this.back.active = true;
		// }, 3000);
		this.updateContent()
		GlobalEvent.instance().on(EventName.SORT_AD_DATA, this.updateContent, this)
		this.content.x = cc.view.getVisibleSize().width;
		cc.tween(this.content).to(3, { x: -634 }).start()

		// this.scrollview$VScrollView.scrollToLeft(3)
		cc.tween(this.plane).to(2, { x: 0 }).start()
		this.headMove();
		SDKManager.getChannel().battleEnd(BattleID.ENTER_EXPORT1_FROM_LOBBY)
		SDKManager.getChannel().battleEnd(BattleID.ENTER_EXPORT1_FROM_LOGIN)

		SDKManager.getChannel().battleStart(BattleID.EXPORT1_RETURN_GAME)

	}
	protected index: number = 0;


	headMove() {
		let time = 0.5;
		let dis = 0.2;
		cc.tween(this.yanchong).repeatForever(
			cc.tween(this.yanchong).by(time, { scaleY: dis }).call(() => {
				let yan = this.yan[this.index++ % this.yan.length]
				yan.scale = 0.5;
				yan.active = true;
				yan.x = - 100
				yan.y = 150;
				cc.tween(yan).to(1.5, { scale: 1, y: yan.y + 100, x: yan.x + 100 }).call(() => {
					yan.active = false;
				}).start()
			}).by(time, { scaleY: -dis })).start();
	}

	updateContent() {
		if (!cc.isValid(this.node)) {
            return;
        }
		for (let index = 0; index < this.itemList.length; index++) {
			this.buffer.push(this.itemList[index])
		}
		this.itemList.length = 0;
		let list = this.model.getAdData()
		for (let index = 0; index < list.length; index++) {
			const element = list[index];
			if (element) {
				let comp = this.buffer.shift()
				if (!comp) {
					let node = cc.instantiate(this.prefab)
					comp = this.controller.initView(node, 'BigExportPage1Item', element) as BigExportPage1Item
					comp.setIndex(index)
					node.y = -200
					this.content.addChild(node)
				} else {
					comp.setIndex(index)
					comp.setModel(element)
					comp.refresh()
				}
				this.itemList.push(comp)
			}

		}
	}


	onDestroy() {
		super.onDestroy()
		GlobalEvent.instance().off(EventName.SORT_AD_DATA, this.updateContent, this)
		SDKManager.getChannel().battleEnd(BattleID.EXPORT1_RETURN_GAME)
		// SDKManager.getChannel().loadAd()
	}

	onBackClick() {
		this.hide()
		this.controller.closeView(ExportType.EXPORT1)
		// this.controller.intoExport3()
	}

	update() {

		this.content.x -= 5
		let width = cc.view.getVisibleSize().width;
		if (this.content.x < - this.content.width - width) {
			this.content.x = width / 2;
		}

	}



}